package com.lucker.controller.version;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lucker.common.util.StringUtil;
import com.lucker.controller.BaseController;
import com.lucker.ds.model.SysVersionConfig;
import com.lucker.framework.constant.ResponseBaseModel;
import com.lucker.framework.constant.ResponseResultModel;
import com.lucker.service.sysversionconfig.SysVersionConfigService;
import com.lucker.vo.VersionUpdateRespMode;


@Controller
@RequestMapping( value = "version")
public class VersionController extends BaseController {

	@Autowired
	SysVersionConfigService sysVersionConfigService;
	
	
	@RequestMapping(value="update",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> commit_opencode(HttpServletRequest req) {
		try {
			String _cversion = getRequestHeader(req).getAppVersion();
			if(StringUtil.isBlank(_cversion)) {
				return new ResponseEntity<>(new ResponseBaseModel(29002," 请求头缺少appVersion这个公共参数 !"), HttpStatus.OK);
			}
			
			String appType = getRequestHeader(req).getAppType();
			if(StringUtil.isBlank(appType)) {
				return new ResponseEntity<>(new ResponseBaseModel(29002," 请求头缺少appType这个公共参数 无法读取配置信息!"), HttpStatus.OK);
			}
			
			SysVersionConfig vConfig = new SysVersionConfig();
			vConfig.setSystemType(appType);
			List<SysVersionConfig> list = sysVersionConfigService.queryAll(vConfig);
			if(CollectionUtils.isEmpty(list)) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,appType + " 这个类型的版本信息没有在后台配置!"), HttpStatus.OK);
			}
			
			
			VersionUpdateRespMode respmode = new VersionUpdateRespMode();
			if(Integer.parseInt(_cversion) < list.get(0).getMinVersionCode() ) {
				respmode.setUpdateType(1);//强制更新
			}
			if(Integer.parseInt(_cversion) >= list.get(0).getMinVersionCode() && Integer.parseInt(_cversion) < list.get(0).getMaxVersionCode()) {
				respmode.setUpdateType(2);//选择性更新
			}
			if(Integer.parseInt(_cversion) >= list.get(0).getMaxVersionCode() ) {
				respmode.setUpdateType(0);//不展示更新提示框
			}
			respmode.setUpdateUrl(list.get(0).getUpdateUrl());
			respmode.setUpdateTexts(list.get(0).getUpdateTexts());
			respmode.setMaxVersionCode(list.get(0).getMaxVersionCode());
			respmode.setMinVersionCode(list.get(0).getMinVersionCode());
			respmode.setCurrVersionCode(Integer.parseInt(_cversion) );
			
			//return new ResponseEntity<>(new ResponseBaseModel(10000,"ok"), HttpStatus.OK);
			return new ResponseEntity<>(new ResponseResultModel<VersionUpdateRespMode>(respmode),HttpStatus.OK);
		}catch(Exception e) {
			this.getLoger().error("登录异常 {} , {}", e.getMessage(),e);
			return new ResponseEntity<>(new ResponseBaseModel(29002,e.getMessage()), HttpStatus.OK);
		}
		
	}
	
}
